﻿
common_guide_control_mgr = 
{
    control_list = {};
    has_checked_ = false;
    init = function ()
        ierd_tgp.tpf_ui.Require("common_guide\\common_model.lua");
        ierd_tgp.tpf_ui.Require("common_guide\\common_control.lua");
    end;

    StartCheckGuid = function()
        if common_guide_control_mgr.has_checked_ == true then
            return;
        end;
        common_guide_control_mgr.has_checked_ = true;
        --[[local game_id = 29;
        local local_cfg_key = "COD.entry_guide";
        local record_item = 31;
        local record_sub_item = "CodGameEntryGuide";
        local guide_url = LoadStr("COD_GUIDE_URL");--"https://www.wegame.com.cn/frames/guide-cod-tips.html";
        local guide_report_type = "COD_entry_guide";
        local use_server_record = 1;
        common_guide_control_mgr.CreateNewGuideControl(game_id, local_cfg_key, record_item,
                record_sub_item, guide_url, guide_report_type, use_server_record);

        game_id = 2000797;
        local_cfg_key = "CFHD.entry_guide";
        record_item = 31;
        record_sub_item = "CFHDGameEntryGuide";
        guide_url = LoadStr("CFHD_GUIDE_URL");--"https://www.wegame.com.cn/frames/guide-cf-tips.html";
        guide_report_type = "CFHD_entry_guide";
        use_server_record = 1;
        common_guide_control_mgr.CreateNewGuideControl(game_id, local_cfg_key, record_item,
                record_sub_item, guide_url, guide_report_type, use_server_record);
        ]]--
    end;

    CreateNewGuideControl = function(game_id, local_cfg_key, record_item, record_sub_item,
            guide_url, guide_report_type, use_server_record, need_cloudgame_permission,
            need_game_permission)
        local guide_control = common_guide_control:new(game_id, local_cfg_key, record_item,
            record_sub_item, guide_url, guide_report_type, use_server_record,
            need_cloudgame_permission, need_game_permission);
        common_guide_control_mgr.control_list[guide_control.key_] = guide_control;
    end;

    on_login_success = function(event_id, json_str)
        common_guide_control_mgr.StartCheckGuid();
        for key in pairs(common_guide_control_mgr.control_list) do
            local guide_control = common_guide_control_mgr.control_list[key];
            guide_control:on_login_success(event_id, json_str);
        end
    end;

    handle_banner_switch = function(event_id, json_str, no_banner_switch)
    base_utility.log("common_guide_control_mgr handle_banner_switch:"
    .. json_str .. ",no_banner_switch:" .. tostring(no_banner_switch));
        if json_str == nil then
            return;
        end
        local params = base_utility.json_to_table(json_str);
        local game_id = nil;
        if params ~= nil then
            if params.module_id ~= nil then
                game_id = tonumber(params.module_id);
            end
        end
        if game_id == nil then
            return;
        end

        for key in pairs(common_guide_control_mgr.control_list) do
            if common_guide_control_mgr.control_list[key] ~= nil then
                common_guide_control_mgr.control_list[key]:change_focus_game(game_id);
            end;
        end

        local common_guide_param = {game_id = game_id};
        local common_guide_configuration =
        comm_center.call_service("SvrTGPConfigMgr_GetCommonGuideConfiguration", common_guide_param);
        if common_guide_configuration == nil or common_guide_configuration.get_data == 0 then
            return;
        end;

        local local_cfg_key = common_guide_configuration.local_cfg_key;
        local record_item = common_guide_configuration.record_item;
        local record_sub_item = common_guide_configuration.record_sub_item;
        local guide_url = common_guide_configuration.guide_url;
        local guide_report_type = common_guide_configuration.guide_report_type;
        local use_server_record = common_guide_configuration.use_server_record;
        local need_cloudgame_permission = common_guide_configuration.need_cloudgame_permission;
        local need_game_permission = common_guide_configuration.need_game_permission;
        local find = false;
        local find_key = "control_" ..tostring(game_id) .. "_local_key_" .. tostring(local_cfg_key)
                        .."_item_" .. tostring(record_item)
                        .. "_sub_item_" ..tostring(record_sub_item);
        for key in pairs(common_guide_control_mgr.control_list) do
            if find_key == key and common_guide_control_mgr.control_list[key] ~= nil then
                find = true;
                common_guide_control_mgr.control_list[key]:handle_banner_switch(event_id, json_str, no_banner_switch);
            end;
        end
        if find == false then
            common_guide_control_mgr.CreateNewGuideControl(game_id, local_cfg_key, record_item,
                    record_sub_item, guide_url, guide_report_type, use_server_record,
                    need_cloudgame_permission, need_game_permission);
            local account = comm_center.call_service("Srv_GetAccountID");
            if account ~= nil then
                if common_guide_control_mgr.control_list[find_key] ~= nil then
                    common_guide_control_mgr.control_list[find_key]:on_get_account_id(
                    account.account_id, game_id, no_banner_switch);
                end;
            end;
        end;
    end;

    on_check_common_guide_record = function(json_str, user_data)
        if common_guide_control_mgr.control_list ~= nil and
           common_guide_control_mgr.control_list[user_data] ~= nil then
           common_guide_control_mgr.control_list[user_data]:on_check_common_guide_record(json_str);
        end;
    end;

    on_set_server_common_guide_cfg =  function(json_str, user_data)
        if common_guide_control_mgr.control_list ~= nil and
           common_guide_control_mgr.control_list[user_data] ~= nil then
           common_guide_control_mgr.control_list[user_data]:on_set_server_common_guide_cfg(json_str);
        end;
    end;

    on_close_main_wnd = function(data)
    end;

    on_cloud_game_cfg_info_brc = function(param)
        if param == nil or param.game_id == nil or param.game_id == 0 then
            return
        end

        if param.no_data == nil or param.no_data == true then
            if param.game_id ~= nil then
                base_utility.log("common_guide_control_mgr on_cloud_game_cfg_info_brc not cloud game:"
                    ..tostring(param.game_id));
            else
                base_utility.log("common_guide_control_mgr on_cloud_game_cfg_info_brc not cloud game nil");
            end;
            return;
        end;
         base_utility.log("common_guide_control_mgr on_cloud_game_cfg_info_brc cloud game:"
                    ..tostring(param.game_id));
       local game_id = tonumber(param.game_id);
        for key in pairs(common_guide_control_mgr.control_list) do
            local guide_control = common_guide_control_mgr.control_list[key];
            if guide_control.common_guide_model_.show_entry_game_id == game_id then
                guide_control:on_cloud_game_cfg_info_brc(game_id);
                return;
            end
        end

        local param_json = "{\"module_id\":" .. tostring(game_id) .. "}";
        common_guide_control_mgr.handle_banner_switch(0, param_json, true);
        for key in pairs(common_guide_control_mgr.control_list) do
            local guide_control = common_guide_control_mgr.control_list[key];
            local game_id = tonumber(param.game_id);
            if guide_control.common_guide_model_.show_entry_game_id == game_id then
                guide_control:on_cloud_game_cfg_info_brc(game_id);
                return;
            end
        end
    end;

    Clear = function()
        common_guide_control_mgr.control_list = {};
    end;
};
common_guide_control_mgr.init();